<template>
  <div class="login-container">
    <el-card class="login-card">
      <el-tabs v-model="activeTab" stretch>
        <el-tab-pane label="登录" name="login">
          <h2 class="login-title">用户登录</h2>
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user">
          <template #prefix>
            <el-icon><User /></el-icon>
          </template>
        </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password>
          <template #prefix>
            <el-icon><Lock /></el-icon>
          </template>
        </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" style="width: 100%">登录</el-button>
        </el-form-item>
      </el-form>

        </el-tab-pane>
        <el-tab-pane label="注册" name="register">
          <h2 class="login-title">用户注册</h2>
          <el-form :model="registerForm" :rules="registerRules" ref="registerFormRef">
            <el-form-item prop="username">
              <el-input v-model="registerForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user">
          <template #prefix>
            <el-icon><User /></el-icon>
          </template>
        </el-input>
            </el-form-item>
            <el-form-item prop="nickname">
              <el-input v-model="registerForm.nickname" placeholder="请输入昵称" prefix-icon="el-icon-user">
          <template #prefix>
            <el-icon><UserFilled /></el-icon>
          </template>
        </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="registerForm.password" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password>
          <template #prefix>
            <el-icon><Lock /></el-icon>
          </template>
        </el-input>
            </el-form-item>
            <el-form-item prop="confirmPassword">
              <el-input v-model="registerForm.confirmPassword" placeholder="请确认密码" prefix-icon="el-icon-lock" show-password>
          <template #prefix>
            <el-icon><Unlock /></el-icon>
          </template>
        </el-input>
            </el-form-item>
            <el-form-item prop="phone">
              <el-input v-model="registerForm.phone" placeholder="请输入手机号" prefix-icon="el-icon-phone">
          <template #prefix>
            <el-icon><Iphone /></el-icon>
          </template>
        </el-input>
            </el-form-item>
            <el-form-item prop="email">
              <el-input v-model="registerForm.email" placeholder="请输入电子邮箱" prefix-icon="el-icon-message">
          <template #prefix>
            <el-icon><Message /></el-icon>
          </template>
        </el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleRegister" style="width: 100%">注册</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { User, Lock, Unlock, Iphone, Message, UserFilled } from '@element-plus/icons-vue'
import {userLoginService, userRegisterService} from '@/api/user.js'
import { ElMessage } from "element-plus";
import { useTokenStore } from '@/stores/token.js'

const router = useRouter()

const activeTab = ref('login')

const loginForm = ref({
  username: '',
  password: ''
})

const registerForm = ref({
  username: '',
  nickname: '',
  password: '',
  confirmPassword: '',
  phone: '',
  email: ''
})

const loginRules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
})

const registerRules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9_]{4,16}$/, message: '用户名必须为4-16位的字母、数字或下划线', trigger: 'blur' }
  ],
  nickname: [
    { required: true, message: '请输入昵称', trigger: 'blur' },
    { pattern: /^[\u4e00-\u9fa5a-zA-Z0-9_]{2,10}$/, message: '昵称必须为2-10位的中文、字母、数字或下划线', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.value.password) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入电子邮箱', trigger: 'blur' },
    { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
  ]
})

const loginFormRef = ref(null)
const registerFormRef = ref(null)

// 登录
const handleLogin = () => {
  loginFormRef.value.validate(async valid => {
    if (valid) {
      let result = await userLoginService(loginForm.value);
      ElMessage.success(result.msg ? result.msg : '登录成功');
      useTokenStore().setToken(result.data);
      router.push('/')
    }
  })
}

// 注册
const handleRegister = () => {
  registerFormRef.value.validate(async valid => {
    if (valid) {
      let result = await userRegisterService(registerForm.value);
      ElMessage.success(result.msg ? result.msg : '注册成功');
      activeTab.value = 'login'
    }
  })
}
</script>

<style lang="scss" scoped>
$primary-gradient: linear-gradient(135deg, rgba(102, 126, 234, 0.8) 0%, rgba(118, 75, 162, 0.8) 100%);
$card-bg: rgba(255, 255, 255, 0.95);
$text-color: #333;

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: $primary-gradient;
  position: relative;
  overflow: hidden;
  
  &::before, &::after {
    content: '';
    position: absolute;
    width: 300px;
    height: 300px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.05);
    z-index: 0;
  }
  
  &::before {
    top: -100px;
    left: -100px;
    animation: float 15s ease-in-out infinite;
  }
  
  &::after {
    bottom: -100px;
    right: -100px;
    animation: float 18s ease-in-out infinite reverse;
  }
  
  @keyframes float {
    0%, 100% { transform: translate(0, 0) rotate(0deg); }
    25% { transform: translate(10px, 10px) rotate(5deg); }
    50% { transform: translate(20px, -10px) rotate(-5deg); }
    75% { transform: translate(-10px, 20px) rotate(5deg); }
  }

  .login-card {
    width: 400px;
    padding: 30px;
    border-radius: 12px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
    background: $card-bg;
    transition: all 0.3s ease;

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 15px 30px rgba(0, 0, 0, 0.3);
    }

    .login-title {
      text-align: center;
      margin-bottom: 30px;
      color: $text-color;
      font-weight: 600;
    }

    .el-input {
      margin-bottom: 20px;
    }

    .el-button {
      margin-top: 10px;
      letter-spacing: 1px;
      transition: all 0.3s;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
      }
    }
  }
}
</style>